package com.java.test;

import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.abi.datatypes.generated.*;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.ContractGasProvider;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class PancakeRouter extends Contract {

    public static final String FUNC_SWAP_EXACT_TOKENS_FOR_TOKENS = "swapExactTokensForTokens";

    protected PancakeRouter(String contractBinary, String contractAddress, Web3j web3j,
                            Credentials credentials, ContractGasProvider gasProvider) {
        super(contractBinary, contractAddress, web3j, credentials, gasProvider);
    }

    public static PancakeRouter load(String contractAddress, Web3j web3j,
                                     Credentials credentials, ContractGasProvider gasProvider) {
        return new PancakeRouter("", contractAddress, web3j, credentials, gasProvider);
    }

    public RemoteFunctionCall<TransactionReceipt> swapExactTokensForTokens(
            BigInteger amountIn,
            BigInteger amountOutMin,
            List<String> path,
            String to,
            BigInteger deadline) {

        // 将路径转换为Address类型列表
        List<Address> pathAddresses = new java.util.ArrayList<>();
        for (String address : path) {
            pathAddresses.add(new Address(address));
        }

        final Function function = new Function(
                FUNC_SWAP_EXACT_TOKENS_FOR_TOKENS,
                Arrays.asList(
                        new Uint256(amountIn),
                        new Uint256(amountOutMin),
                        new DynamicArray<>(Address.class, pathAddresses),
                        new Address(to),
                        new Uint256(deadline)
                ),
                Collections.emptyList());

        return executeRemoteCallTransaction(function);
    }

    // 其他Router函数可以类似实现...
}
